/**
 * Author : Jorge Mejuto Create : 19/05/2009
 */

package net.eyra.negocio.dao;

import java.util.List;

import net.eyra.negocio.entidades.gestion.direccion.Direccion;
import net.eyra.negocio.entidades.gestion.direccion.Pais;
import net.eyra.negocio.entidades.gestion.direccion.Provincia;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class DireccionDao {

    public Direccion getDireccion(int id_direccion) throws EyraDaoException {
        Session session = null;
        Direccion direccion = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            direccion = (Direccion) session.get(Direccion.class, id_direccion);
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return direccion;
    }

    public List<Pais> getPaises() throws EyraDaoException {
        Session session = null;
        List<Pais> _lista = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            _lista = session.createCriteria(Pais.class).addOrder(Order.asc("nombre")).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return _lista;
    }

    public List<Provincia> getProvincias() throws EyraDaoException {
        Session session = null;
        List<Provincia> _lista = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            _lista = session.createCriteria(Provincia.class).addOrder(Order.asc("pais.id_pais")).addOrder(Order.asc("nombre")).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return _lista;
    }

    public List<Provincia> getProvincias(int id_pais) throws EyraDaoException {
        Session session = null;
        List<Provincia> _lista = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            _lista = session.createCriteria(Provincia.class).add(Restrictions.eq("pais.id_pais", id_pais)).addOrder(Order.asc("nombre")).list();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return _lista;
    }

    /**
     * Insertar o actualizar una direccion
     * 
     * @param direccion
     * @throws DaoException
     * @throws EyraDaoException
     */
    public Direccion setDireccion(Direccion direccion) throws EyraDaoException {
        Session session = null;
        try {
            session = HibernateUtil.getSession();
            session.beginTransaction().begin();
            session.saveOrUpdate(direccion);
            session.flush();
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            if (session != null) session.beginTransaction().rollback();
            throw new EyraDaoException(e);
        } finally {
            if (session != null && session.isOpen()) session.close();
        }
        return direccion;
    }

    public void anularDireccion(int id_direccion) throws EyraDaoException {
        Direccion direccion = getDireccion(id_direccion);
        direccion.setActiva("N");
        setDireccion(direccion);
    }
}
